package com.audiocn.poster;

import java.io.File;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.WindowManager;
import android.widget.Toast;

import com.audiocn.Utils.Log;
import com.audiocn.Utils.Utils;
import com.audiocn.dc.InitDC;
import com.audiocn.engine.DCEngine;
import com.audiocn.manager.BaseManager;
import com.audiocn.manager.PostersManager;

public class Application extends Activity {
	
	public static DCEngine dcEngine;
	public static Application application;
	long timeForAnimator; //系统的当前时间
	static Context context;
	public static PostersManager postersManager;
	
	public static BaseManager currManager;
	
	long lastTime = 0;
	
	AlertDialog ad = null;
	
	NotificationManager mNM;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		application = this;
		context = this;
		mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		Utils.doDefaultTaoBaoSid(this);
		stopServiceRun();
		dcEngine = new DCEngine(context);
		setContentView(dcEngine.getContentView());
		postersManager = new PostersManager(context);
		timeForAnimator = System.currentTimeMillis();
		dcEngine.initDC(new InitDC(context));
	}
	public void enterMainDC() {
		if (Utils.isSDCard() && Utils.isSDCardFree()) {
			init();
		} else {
			if (!Utils.isSDCard()) {
				Log.e("isSDCard", "isSDCard");
				showTipDialog();
			} else {
				init();
				showConfirmDialog();
			}
		}
	}
	private void init() {
		try {
			File file = new File(Configs.tlcyPath);
			if (!file.isDirectory()) {
				file.mkdir();
			}
			long sleep = System.currentTimeMillis() - timeForAnimator;
			if (sleep < 1000) {
				Thread.sleep(1000 - sleep);
			}
		} catch (Exception e) {
			Toast.makeText(context, e.getMessage(), 1).show();
		}
		currManager = postersManager;
		dcEngine.setCurrentManager(currManager);
		dcEngine.showDC(currManager.getMainDC(), 0);
	}
	long lastKeyDown = 0;

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_MENU) {
			return super.onKeyDown(keyCode, event);
		} else if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN
				|| keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
			return super.onKeyDown(keyCode, event);
		} else if (keyCode == KeyEvent.KEYCODE_BACK) {
			if (currManager != null) {
				if (currManager.back()) {
					showExit();
				}
			}
			return true;
		} else if (keyCode == KeyEvent.KEYCODE_CALL) {
			return true;
		}
		return true;
	}

	@Override
	protected void onResume() {
		super.onResume();
		lastKeyDown = System.currentTimeMillis();
	}

	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		if (event.getKeyCode() == KeyEvent.KEYCODE_MENU
				|| event.getKeyCode() == KeyEvent.KEYCODE_SEARCH) {
			return true;
		}
		return super.dispatchKeyEvent(event);
	}

	@Override
	public boolean onTrackballEvent(MotionEvent event) {
		return true;
	}


	/**
	 * 显示退出提示 2010-11-2
	 * 
	 * @author wang wei
	 */
	public void showExit() {
		AlertDialog d = new AlertDialog.Builder(this).setMessage("您确认要退出比物连类吗?").setPositiveButton("确定", new OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				onDestroy();
			}
		}).setNegativeButton("取消", new OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		}).create();
		d.show();
	}
	/**
	 * 显示退出提示 2010-11-2
	 * 
	 * @author wang wei
	 */
	public void showConfirmDialog() {
		Log.e("showConfirmDialog", "showConfirmDialog");
		if (ad != null && !ad.isShowing()) {
			ad = new AlertDialog.Builder(this).setTitle(context.getString(R.string.userTip))
			.setMessage(context.getString(R.string.confirmMsg)).setPositiveButton(R.string.userTipOk, new OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
					dialog.cancel();
				}
			}).setNegativeButton(R.string.userTipCancel, new OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
					onDestroy();
				}
			}).create();
			ad.show();
		}
	}
	public void showTipDialog() {
		Log.e("showConfirmDialog", "showConfirmDialog");
		ad = new AlertDialog.Builder(this)
		.setMessage("检测不到SDCARD,比物连类无法启动..").setPositiveButton(R.string.userTipOk, new OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
				onDestroy();
			}
		}).create();
		ad.show();
	}

	@Override
	public void onDestroy() {
		try {
			Intent intent = new Intent(this, TLCYClearService.class);
			this.startService(intent);
			super.onDestroy();
		} catch (Exception e) {
		}
		System.exit(0);
	}
	protected void onStop() {
		customNotification();
		super.onStop();
	}
	public void customNotification() {
		Intent intent = new Intent(this, Application.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
		PendingIntent contentIntent = PendingIntent.getActivity(this, 10, intent, 2);
		
		Notification nf = new Notification();
		nf.icon = R.drawable.icon_title;
		nf.contentIntent = contentIntent;
		nf.flags = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_AUTO_CANCEL;
		nf.setLatestEventInfo(context, "比物连类", "比物连类", contentIntent);
		mNM.notify(R.layout.posters_list, nf);
	}
	
	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);
		this.finish();
		this.onDestroy();
		android.os.Process.killProcess(android.os.Process.myPid());
	}

	public void stopServiceRun() {
		TLCYClearService.isGoOn = false;
		Log.e("stopServiceRun", "stopServiceRun");
		if (TLCYClearService.thread != null) {
			try {
				TLCYClearService.thread.interrupt();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public static int getLayoutId(int oldId) {
		int layout = oldId;
		switch (oldId) {
		default:
			break;
		}

		return layout;
	}
	
}